﻿using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BaseDAL<T> : IBaseDAL<T> where T : class
    {
        public BaseDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public async Task<int> AddAsync(T t)
        {
            await  Context.Set<T>().AddAsync(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> AddRangeAsync(List<T> t)
        {
            await Context.Set<T>().AddRangeAsync(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T t)
        {
            Context.Set<T>().Remove(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteRangeAsync(List<T> t)
        {
            Context.Set<T>().RemoveRange(t);
            return await Context.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return Context.Set<T>();
        }

        public async Task<int> UpateAsync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> UpateRangeAsync(List<T> t)
        {
            Context.Set<T>().UpdateRange(t);
            return await Context.SaveChangesAsync();
        }
    }
}
